package com.zeedev.flyingsaucer.beers.activities.home.impl;

import android.app.Activity;
import android.content.Intent;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.name.Named;
import com.zeedev.flyingsaucer.beers.activities.ActivitiesConstants;
import com.zeedev.flyingsaucer.beers.activities.about.AboutActivity;
import com.zeedev.flyingsaucer.beers.activities.beerlists.BeerTree;
import com.zeedev.flyingsaucer.beers.activities.home.interfaces.HomeController;
import com.zeedev.flyingsaucer.beers.activities.tabchooser.BeerListParent;
import com.zeedev.flyingsaucer.modules.BeerDatabaseModule.impl.members.MemberBeerListModel;
import com.zeedev.flyingsaucer.modules.BeerDatabaseModule.impl.stores.StoreBeerListModel;
import com.zeedev.flyingsaucer.modules.DownloadModule.interfaces.DownloadManager;
import mobi.zeedev.flyingsaucer.common.CredentialsModule.interfaces.ICredentials;
import mobi.zeedev.flyingsaucer.common.DataModule.BeerConstants;
import mobi.zeedev.flyingsaucer.common.DataModule.FlyingSaucerConv;
import mobi.zeedev.flyingsaucer.common.WebLinkModule.interfaces.UfoClubClient;
import roboguice.inject.ContextScoped;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

/**
 * User: James Johnson
 * Date: Oct 29, 2010
 * Time: 7:17:56 PM
 */
@ContextScoped
public class HomeControllerImpl implements HomeController {

    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    // Class Injections
    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    @Inject
    Provider<Activity> mActivity;

    private HomeModel model;

    @Inject
    DownloadManager downloadManager;

    @Inject
    UfoClubClient client;

    @Inject
    @Named("autoPreferenceCredentials")
    Provider<ICredentials> credentials;


    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    // Constants
    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    @Inject
    public HomeControllerImpl(HomeModel model) {
        this.model = model;
/*        for (PropertyChangeListener pcl : model.pcs.getPropertyChangeListeners() ) {
            model.pcs.removePropertyChangeListener(pcl);
        }*/
        model.pcs.addPropertyChangeListener(new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                String source = evt.getPropertyName();
                HomeModel.Properties p = HomeModel.Properties.valueOf(source);
                switch (p) {
                    case REFRESHING:
                        changeProgressIndicator((Boolean) evt.getNewValue());
                        break;
//                    case HOME_INFO:
//                        ((HomeActivity) mActivity.get()).updateViews();
//                        TODO FIX THIS ??? lol
//
//                        break;
                    case CREDENTIALS:
                        //nothing
                        break;
                }


            }
        });
    }

    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    // GUI Injections
    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX


    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    // Interface
    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    public void init() {

//        if (!credentials.get().permittedLogin()) {
////            viewLogin();
////            return;
//            mActivity.get().setResult(Activity.RESULT_CANCELED);
//            mActivity.get().finish();
//
//
//        }



//        refresh(false);
    }

    @Override
    public void logout() {
//        credentials.reset();
//        model.getHomeInfo().reset();
        client.logout();

        //        viewLogin();

        mActivity.get().setResult(ActivitiesConstants.LOGOUT_HOME_SCREEN);
        mActivity.get().finish();

    }


    @Override
    public HomeModel getModel() {
        return model;
    }

    @Override
    public void viewNewBeers() {

        Intent i2 = new Intent(mActivity.get().getBaseContext(), BeerTree.class);
        i2.putExtra(BeerConstants.INTENT_BEERTREE_MODE, FlyingSaucerConv.Members.MemberBeer.SIMPLE_CREATION_DATE);
        i2.putExtra(BeerConstants.INTENT_BEER_DB, MemberBeerListModel.class);

        mActivity.get().startActivityForResult(i2, ActivitiesConstants.VIEW_NEW_BEERS_SCREEN);

    }

    @Override
    public void viewBeers() {
        Intent intent = new Intent(mActivity.get(), BeerListParent.class);
        intent.putExtra(BeerConstants.INTENT_CONTROLLER_CLASS, MemberBeerListModel.class);
        intent.putExtra(BeerConstants.INTENT_TAB_MODEL, "MemberTabModel");
        mActivity.get().startActivityForResult(intent, ActivitiesConstants.VIEW_BEERS_SCREEN);

    }

    @Override
    public void viewStores() {
//        Toast.makeText(mActivity.get(), "Other stores does not work.", Toast.LENGTH_SHORT).show();
        //TODO fix above
//        Intent intent = new Intent(mActivity.get(), StoreChooser.class);
//        mActivity.get().startActivity(intent);
        Intent intent = new Intent(mActivity.get(), BeerListParent.class);
        intent.putExtra(BeerConstants.INTENT_CONTROLLER_CLASS, StoreBeerListModel.class);
        intent.putExtra(BeerConstants.INTENT_TAB_MODEL, "StoreTabModel");
        mActivity.get().startActivityForResult(intent, ActivitiesConstants.VIEW_BEERS_SCREEN);

    }

/*    @Override
    public void refresh(Boolean always) {
        if ((always || model.getHomeInfo().isEmpty() || model.getHomeInfo().shouldRefresh() || (!model.getHomeInfo().get(HomeInfo.E_MAIL).equals(credentials.get().getUserName()))) && !model.isRefreshing()) {
            downloaderLogin.get().execute();
            downloaderMemberId.get().execute();
        }
        *//*else {
            ((HomeActivity)mActivity.get()).updateViews();
        }*//*
    }*/

    @Override
    public void viewAbout() {
        Intent i = new Intent(mActivity.get(), AboutActivity.class);
        mActivity.get().startActivityForResult(i, ActivitiesConstants.VIEW_ABOUT_DIALOG_SCREEN);
    }

    @Override
    public void minimizeUfoClubApp() {

        //the home screen can stay in the "background foucs"

        mActivity.get().startActivity(new Intent().setAction(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME));



//        mActivity.get().setResult(ActivitiesConstants.MINIMIZE_HOME_SCREEN);
//        mActivity.get().finish();



    }


    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    // View Controllers
    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX


    private void changeProgressIndicator(Boolean bin) {
        mActivity.get().setProgressBarIndeterminateVisibility(bin);
    }


}



